from sqlmodel import SQLModel, Field, Index
from typing import Optional
from datetime import datetime

class User(SQLModel, table=True):
    """用户表数据库模型"""
    __tablename__ = "t_user"  # 对应数据库表名
    
    # 索引定义（优化查询性能）
    __table_args__ = (
        Index("idx_mobile", "mobile", unique=True, info="手机号唯一索引"),
        Index("idx_invite_code", "inviteCode", unique=True, info="邀请码唯一索引"),
    )
    
    uid: Optional[int] = Field(default=None, primary_key=True, index=True, description="主键，自增ID")
    mobile: str = Field(default="", max_length=32, description="用户手机号")
    nickname: str = Field(default="", max_length=64, description="用户昵称")
    avatarUrl: str = Field(default="", max_length=255, description="头像URL地址")
    inviteCode: str = Field(default="", max_length=10, description="邀请码")
    status: int = Field(default=0, description="状态 0、正常 1、异常")
    isSuper: int = Field(default=0, description="是否超级用户 0、否 1、是")
    createdAt: datetime = Field(default_factory=datetime.now, description="创建时间")
    updatedAt: datetime = Field(default_factory=datetime.now, sa_column_kwargs={"onupdate": datetime.now}, description="更新时间")